package com.bcx.crm.java.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.logging.Logger;

/**
 * Created by ys on 2018/1/3.
 * 统计在线的人数
 */

public class OnlineCount implements HttpSessionListener {

    private int count=0;//记录session的数量
    private Logger logger=Logger.getLogger(this.getClass().getName());

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) { //监听session的创建
        count++;
        httpSessionEvent.getSession().getServletContext().setAttribute("Count",count);
        logger.info("当前在线人数为:["+count+"]");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        count--;
        httpSessionEvent.getSession().getServletContext().setAttribute("Count",count);
        logger.info("当前在线人数为:["+count+"]");
    }
}
